package com.rmdjzz.slmmn.controller.user;

import com.rmdjzz.slmmn.pojo.base.Singer;
import com.rmdjzz.slmmn.service.SingerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @作者 张绍鑫
 * @创建时间 2023/5/27
 */

@Controller
@Api(tags = "用户首页")
@RequestMapping("/user")
@Slf4j
public class UserIndex {
    @Autowired
    private SingerService singerService;
    @GetMapping("/userIndex")
    @ApiOperation("用户首页")
    public String dataAnalysis(Model model){
        List<Singer> list = singerService.list();
        int size = list.size();
        //获取一个随机数
        //将随机数和集合里面的数据进行对比，如果与集合所有数据不同则添加
        //如果相同则递归，直到集合长度为5结束
        if (size>5){
            ArrayList<Integer> item = new ArrayList<>();
            Random random = new Random();
            for (int i=0;i<5;i++){
                listNum(random,item,size);
            }
            ArrayList<Singer> singers = new ArrayList<>();
            for (Integer integer:item){
                singers.add(list.get(integer));
            }

            model.addAttribute("list",singers);

        }else {
            model.addAttribute("list",list);
        }
        return "indexUser";
    }

    public List<Integer> listNum(Random random,List<Integer> item,Integer size){
        log.info("length:"+item.size());
        int i = random.nextInt(size);
        boolean flag=true;
        for (Integer m:item){
            if(m==i){
                flag=false;
            }
        }
        if (flag){
            item.add(i);
        }else {
            listNum(random,item,size);
        }
        return item;
    }

}
